﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace SpiderPlatform
{
    public class MatchServie
    {
       // https://ras.ocjic.com/ras/EhALoxaZxXtfh1M68Qt1SVX8/pdxl.jpg
        public async Task doWorkAsync()
        {
            byte[] data = await downloadImageAsync("https://cdn.yamibuy.net/item/84a45777897bcd2db1714123d5cb645b_370x370.webp");
            var baseb4=Convert.ToBase64String(data);
            var info=GetMatchInfo(baseb4);
            Console.WriteLine(info);
        }

        /// <summary>
        /// 根据base64字符串返回一个封装好的GDI+位图。
        /// </summary>
        /// <param name="base64string">可转换成位图的base64字符串。</param>
        /// <returns>Bitmap对象。</returns>
        public Bitmap GetImageFromBase64(string base64string)
        {
            byte[] b = Convert.FromBase64String(base64string);
            MemoryStream ms = new MemoryStream(b);
            Bitmap bitmap = new Bitmap(ms);
            return bitmap;
        }

        /// <summary>
        /// 将图片转换成base64字符串。
        /// </summary>
        /// <param name="imagefile">需要转换的图片文件。</param>
        /// <returns>base64字符串。</returns>
        public string GetBase64FromImage(string imagefile)
        {
            string strbaser64 = "";
            try
            {
                Bitmap bmp = new Bitmap(imagefile);
                using (MemoryStream ms = new MemoryStream())
                {
                    bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                    byte[] arr = new byte[ms.Length];
                    ms.Position = 0;
                    ms.Read(arr, 0, (int)ms.Length);
                    ms.Close();
                    strbaser64 = Convert.ToBase64String(arr);
                }
            }
            catch (Exception)
            {
                throw new Exception("Something wrong during convert!");
            }

            return strbaser64;
        }  

        private async Task<byte[]> downloadImageAsync(string url)
        {
            HttpClient client = new HttpClient();
            byte[] img=await client.GetByteArrayAsync(url);
            return img;
        }

        private string GetMatchInfo(string base64)
        {       
            WebClient webClient = new WebClient();
            webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); 
            webClient.Headers.Add("Authorization", "APPCODE 14b50164714346d18fc6c600e7b6951f");
            string parm = "src="+ base64;
            byte[] postData = Encoding.UTF8.GetBytes(parm);//编码，尤其是汉字，事先要看下抓取网页的编码方式     
            byte[] responseData = webClient.UploadData("http://goodsdect.market.alicloudapi.com/goodsdect", "POST", postData);//得到返回字符流  
            string data = Encoding.UTF8.GetString(responseData);//解码 
            return data;
        }
    }

    public class MatchResult {

        public int status { get; set; }

        public string msg { get; set; }
    }
}
